package com.zhiyou100.taxi.acction;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhiyou100.taxi.entity.CarIn;
import com.zhiyou100.taxi.entity.Data;
import com.zhiyou100.taxi.service.CarInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-27 22:42
 * @Description
 */
@Controller
@RequestMapping("/view")
public class ViewEcharts {
    @Autowired
    CarInService carInService;

    @RequestMapping("/yearMarket.action")
    public String showYearMarket() {
        return "stats/year-market";
    }

    @ResponseBody
    @RequestMapping("/viewMarket.action")
    public Object viewMarket() {

        return null;
    }

    @ResponseBody
    @RequestMapping("/viewCustomer.action")
    public Object viewCustomer() {
        List<CarIn> list = carInService.getAll();
        Data.CustomerData customerData = new Data().getInstanceCustomer();

        HashMap<String, Integer> map = new HashMap<>();

        list.forEach(car -> {
            Date time = car.getCarInTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy");
            String date = format.format(time);
            if (map.containsKey(date)) {
                map.put(date, map.get(date) + 1);
            } else {
                map.put(date, 1);
            }
        });

        Set<String> set = map.keySet();
        customerData.setYear(set);
        ArrayList<Integer> count = new ArrayList<>();
        set.forEach(k -> {
            Integer value = map.get(k);
            count.add(value);
        });
        customerData.setCount(count);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String json = objectMapper.writeValueAsString(customerData);
            return json;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/yearCustomerIncrement.action")
    public String showYearCustomerIncrement() {
        return "stats/year-customer-increment";
    }
}
